<?php
namespace Common\Library\COM;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ImageDo
 *
 * @author Administrator
 */
class ImageDo{
    //put your code here



    public static function CompositePicture(){
        $image = new \Think\Image();
        $image->open('./1.jpg')->water('./logo.png',\Think\Image::IMAGE_WATER_NORTHWEST)->save("water.jpg");
    }


    //二维码合成
    public static function getImgQrcode($url,$qrcode_path){

        Vendor('phpqrcode.phpqrcode');
        //生成二维码图片
        $object = new \QRcode();
        $content= empty($url)?C('SYS_SITE'):$url;//网址或者是文本内容

        $errorCorrectionLevel = 3;//容错级别
        $matrixPointSize = 6;//生成图片大小
        $matrixMarginSize = 2;//边距大小
        $qrcode_path_new = './Public/location/qecode/'.mt_rand(100,999).'.png';//二维码图片路径
        //生成二维码图片
        \QRcode::png($content,$qrcode_path_new, $errorCorrectionLevel, $matrixPointSize, $matrixMarginSize);
        $QR = $qrcode_path_new;//已经生成的原始二维码图
        if(!file_exists($qrcode_path_new)){
            return false;
        }
        $logo = './Public/location/Images/logo.6.png';//准备好的logo图片

        $QR = imagecreatefromstring(file_get_contents($QR));
        $logo = imagecreatefromstring(file_get_contents($logo));
        $QR_width = imagesx($QR);//二维码图片宽度
        $QR_height = imagesy($QR);//二维码图片高度
        $logo_width = imagesx($logo);//logo图片宽度
        $logo_height = imagesy($logo);//logo图片高度
        $logo_qr_width = $QR_width / 5;
        $scale = $logo_width/$logo_qr_width;
        $logo_qr_height = $logo_height/$scale;
        $from_width = ($QR_width - $logo_qr_width) / 2;

        //重新组合图片并调整大小
        $adb = imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
        $logo_qr_height, $logo_width, $logo_height);
        //输出图片
        //header("Content-type: image/png");
        imagepng($QR,$qrcode_path);
        imagedestroy($QR);
        return $qrcode_path;

    }


    /**
     *
     * 图片的等比缩放
     * @param unknown_type $im			 原图，图片对象
     * @param unknown_type $maxwidth 	定义生成图片的最大宽度（单位：像素）
     * @param unknown_type $maxheight	生成图片的最大高度（单位：像素）
     * @param unknown_type $name		生成的图片名
     * @param unknown_type $filetype	最终生成的图片类型(.jpg/.png/.gif)
     */
    protected function ResizeImage($im, $maxwidth, $maxheight, $name, $filetype, $avatar_path) {

        $pic_width = imagesx($im);
        $pic_height = imagesy($im);

        if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {
            if ($maxwidth && $pic_width > $maxwidth) {
                $widthratio = $maxwidth / $pic_width;
                $resizewidth_tag = true;
            }

            if ($maxheight && $pic_height > $maxheight) {
                $heightratio = $maxheight / $pic_height;
                $resizeheight_tag = true;
            }

            if ($resizewidth_tag && $resizeheight_tag) {
                if ($widthratio < $heightratio)
                    $ratio = $widthratio;
                else
                    $ratio = $heightratio;
            }

            if ($resizewidth_tag && !$resizeheight_tag)
                $ratio = $widthratio;
            if ($resizeheight_tag && !$resizewidth_tag)
                $ratio = $heightratio;

            $newwidth = $pic_width * $ratio;
            $newheight = $pic_height * $ratio;

            if (function_exists("imagecopyresampled")) {
                $newim = imagecreatetruecolor($newwidth, $newheight);
                imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
                //echo 'imagecopyresampled@'.$newwidth.'@'.$newheight.'@'.$pic_width.'@'.$pic_height;
            } else {
                $newim = imagecreate($newwidth, $newheight);
                imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
                //echo 'imagecopyresized';
            }

            $name = $avatar_path . '/' . $name . $filetype;
            imagepng($newim, $name);
            imagedestroy($newim);
        } else {
            $name = $avatar_path . '/' . $name . $filetype;
            imagepng($im, $name);
        }
    }

}
